import time

from selenium.webdriver.common.by import By

from common.base import Base


class AddGoodsPage(Base):
    # 导航栏
    def navigation_bar(self):
        self.switch_to_frame((By.ID, 'menu-frame'))
        self.click((By.CLASS_NAME, 'icon-goods'))

    # 添加商品页frame切换
    # def iframe_add_goods(self):

    # 添加商品按钮
    def add_new_goods(self):
        self.switch_to_default_content()
        self.switch_to_frame((By.ID, 'main-frame'))
        self.click((By.LINK_TEXT, '添加新商品'))

    # 通用信息
    def general_information(self, input_content, index, path):
        # 通用信息按钮
        self.click((By.ID, 'general-tab'))
        # 商品名称
        self.send_keys((By.NAME, 'goods_name'), input_content)
        # 字体颜色
        self.click((By.XPATH, '//div[@id="font_color"]'))
        # self.click((By.XPATH, '//td[@bgcolor="#008002"]'))
        elements = self.driver.find_elements(By.XPATH, '//td[@style="border: 1px solid rgb(189, 187, 188);"]')
        for element in elements:
            self.driver.find_element(By.XPATH, '//*[@id="font_color"]').click()
            time.sleep(1)
            element.click()
            self.click((By.XPATH, '//*[@id="font_color"]'))
        # 字体样式
        self.select_by_index((By.XPATH, '//*[@id="general-table"]/tbody/tr[1]/td[2]/select'), index)
        # 商品货号
        self.send_keys((By.NAME, 'goods_sn'), input_content)
        # 选择分类
        self.select_by_index((By.NAME, 'cat_id'), index)
        # 添加分类
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="general-table"]/tbody/tr[3]/td[2]/button'))
        # 分类名
        self.send_keys((By.NAME, 'addedCategoryName'), input_content)
        # 确定分类
        self.click((By.XPATH, '//*[@id="category_add"]/button[1]'))
        # 弹窗取消
        # time.sleep(2)
        # self.alter_accept()
        # 分类管理
        # self.click((By.XPATH, '//button[@onclick="return goCatPage()"]'))

        # 扩展添加
        time.sleep(2)
        self.click((By.XPATH, '//input[@onclick="addOtherCat(this.parentNode)"]'))
        # 扩展选择
        self.select_by_index((By.NAME, 'other_cat[]'), index)
        # 品牌选择
        self.select_by_index((By.NAME, 'brand_id'), index)
        # 添加品牌
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="general-table"]/tbody/tr[5]/td[2]/button'))
        # 输入品牌
        self.send_keys((By.NAME, 'addedBrandName'), input_content)
        # 确定品牌
        self.click((By.XPATH, '//*[@id="brand_add"]/button[1]'))
        # 品牌管理
        # self.click((By.XPATH, '//button[@onclick="return goBrandPage()"]'))
        # 弹窗取消
        time.sleep(2)
        # self.alter_dismiss()
        # 选择供应商
        self.select_by_index((By.ID, 'suppliers_id'), index)
        # 本店售价
        self.clear((By.NAME, 'shop_price'))
        self.send_keys((By.NAME, 'shop_price'), input_content)
        # 注册用户
        self.clear((By.ID, 'rank_1'))
        self.send_keys((By.ID, 'rank_1'), input_content)
        # 待销用户
        self.clear((By.ID, 'rank_3'))
        self.send_keys((By.ID, 'rank_3'), input_content)
        # vip
        self.clear((By.ID, 'rank_2'))
        self.send_keys((By.ID, 'rank_2'), input_content)
        # 优惠数量
        self.send_keys((By.NAME, 'volume_number[]'), input_content)
        # 优惠价格
        self.send_keys((By.NAME, 'volume_price[]'), input_content)
        # 市场价格
        self.clear((By.NAME, 'market_price'))
        self.send_keys((By.NAME, 'market_price'), input_content)
        # 取整数
        self.click((By.XPATH, '//input[@onclick="integral_market_price()"]'))
        # 虚拟销量
        self.clear((By.NAME, 'virtual_sales'))
        self.send_keys((By.NAME, 'virtual_sales'), input_content)
        # 赠送消费积分数
        self.clear((By.NAME, 'give_integral'))
        self.send_keys((By.NAME, 'give_integral'), input_content)
        # 赠送等级积分数
        self.clear((By.NAME, 'rank_integral'))
        self.send_keys((By.NAME, 'rank_integral'), input_content)
        # 积分购买金额
        self.clear((By.XPATH, '//*[@id="general-table"]/tbody/tr[14]/td[2]/input'))
        self.send_keys((By.XPATH, '//*[@id="general-table"]/tbody/tr[14]/td[2]/input'), input_content)
        # 促销价
        self.click((By.ID, 'is_promote'))
        # 促销价输入
        self.clear((By.ID, 'promote_1'))
        self.send_keys((By.ID, 'promote_1'), input_content)
        # 促销开始日期
        self.click((By.ID, 'selbtn1'))
        # 日期选择
        self.click((By.XPATH, '/html/body/div[5]/table/tbody/tr[4]/td[5]'))
        # 促销结束日期
        self.click((By.ID, 'selbtn2'))
        # 日期选择
        self.click((By.XPATH, '/html/body/div[6]/table/tbody/tr[4]/td[4]'))
        # 上传商品图片
        self.upadte_file((By.XPATH, '//*[@id="general-table"]/tbody/tr[17]/td[2]/div/input'), path)
        # 文件地址
        self.send_keys((By.XPATH, '//*[@id="general-table"]/tbody/tr[17]/td[2]/input'), path)
        # 上传商品缩略图
        self.upadte_file((By.XPATH, '//*[@id="auto_thumb_3"]/div/input'), path)
        # 文件地址
        self.send_keys((By.XPATH, '//*[@id="auto_thumb_3"]/input'), path)
        # 自动生成缩略图
        self.click((By.ID, 'auto_thumb'))

    # 详细描述
    def detailed_information(self, text):
        # 详细描述点击
        self.click((By.ID, 'detail-tab'))
        # 再下一级
        self.switch_to_frame((By.ID, 'goods_desc___Frame'))
        # 再下一级
        frame = self.switch_to_frame((By.XPATH, '//*[@id="xEditingArea"]/iframe'))
        self.send_keys((By.XPATH, '/html/body'), text)
        self.switch_to_default_content()
        self.switch_to_frame((By.ID, 'main-frame'))

    # 其他信息
    def other_information(self, content, index, keyword):
        # 其他信息按钮
        self.click((By.ID, 'mix-tab'))
        # 商品重量
        self.send_keys((By.NAME, 'goods_weight'), content)
        # 重量单位
        self.select_by_index((By.NAME, 'weight_unit'), index)
        # 商品库存
        self.send_keys((By.XPATH, '//*[@id="mix-table"]/tbody/tr[2]/td[2]/input'), content)
        # 库存警告
        self.send_keys((By.XPATH, '//*[@id="mix-table"]/tbody/tr[3]/td[2]/input'), content)
        # 精品
        self.click((By.NAME, 'is_best'))
        # 新品
        self.click((By.NAME, 'is_new'))
        # 热销
        self.click((By.NAME, 'is_hot'))
        # 上架
        self.click((By.NAME, 'is_on_sale'))
        # 能作为普通商品销售
        self.click((By.NAME, 'is_alone_sale'))
        # 是否为免运费商品
        self.click((By.NAME, 'is_shipping'))
        # 商品关键词
        self.send_keys((By.NAME, 'keywords'), keyword)
        # 商品简单描述
        self.send_keys((By.XPATH, '//*[@id="mix-table"]/tbody/tr[9]/td[2]/textarea'), content)
        # 商家备注
        self.send_keys((By.XPATH, '//*[@id="mix-table"]/tbody/tr[10]/td[2]/textarea'), content)

    # 商品属性
    def commodity_property(self, index, content):
        self.click((By.ID, 'properties-tab'))
        # 选择类型
        self.select_by_index((By.XPATH, '//*[@id="properties-table"]/tbody/tr[1]/td[2]/select'), index)
        # 作者
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[1]/td[2]/input[2]'), content)
        # 出版社
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[2]/td[2]/input[2]'), content)
        # 书号
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[3]/td[2]/input[2]'), content)
        # 出版日期
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[4]/td[2]/input[2]'), content)
        # 开本
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[5]/td[2]/input[2]'), content)
        # 图书页数
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[6]/td[2]/input[2]'), content)
        # 装订选择
        self.select_by_index((By.XPATH, '//*[@id="attrTable"]/tbody/tr[7]/td[2]/select'), index)
        # 规格
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[8]/td[2]/input[2]'), content)
        # 版次
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[9]/td[2]/input[2]'), content)
        # 印张
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[10]/td[2]/input[2]'), content)
        # 字数
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[11]/td[2]/input[2]'), content)
        # 分类
        self.send_keys((By.XPATH, '//*[@id="attrTable"]/tbody/tr[12]/td[2]/input[2]'), content)

    # 商品相册
    def goods_photo_album(self, path):
        self.click((By.ID, 'gallery-tab'))
        time.sleep(5)
        self.upadte_file(
            (By.XPATH, '/html/body/div[1]/div[2]/form/table[5]/tbody/tr[1]/td[2]/div/div/div/div[2]/input'), path)

    # 关联商品
    def associated_goods(self, index, content):
        time.sleep(3)
        # 点击关联商品
        self.click((By.ID, 'linkgoods-tab'))
        # 分类索引
        self.select_by_index((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[1]/td/select[1]'), index)
        # 品牌索引
        self.select_by_index((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[1]/td/select[2]'), index)
        # 文本输入
        self.send_keys((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[1]/td/input[1]'), content)
        # 搜索点击
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[1]/td/input[2]'))
        # 下拉栏选择
        self.select_by_index((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[1]/select'), index)
        # 双向关联
        time.sleep(1)
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[2]/p[1]/input[2]'))
        # 单向关联
        time.sleep(1)
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[2]/p[1]/input[1]'))
        # 前进
        time.sleep(1)
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[2]/p[3]/input'))
        # 选择商品
        time.sleep(1)
        self.select_by_index((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[3]/select'), index)
        # 回退
        time.sleep(1)
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[2]/p[3]/input'))
        # 双进
        time.sleep(1)
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[2]/p[2]/input'))
        # 选择商品
        time.sleep(1)
        self.select_by_index((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[3]/select'), index)
        # 回退
        time.sleep(1)
        self.click((By.XPATH, '//*[@id="linkgoods-table"]/tbody/tr[3]/td[2]/p[5]/input'))

    # 配件
    def mountings(self, index, content, price):
        # 点击配件
        self.click((By.ID, 'groupgoods-tab'))
        # 分类索引
        self.select_by_index((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[1]/td/select[1]'), index)
        # 品牌索引
        self.select_by_index((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[1]/td/select[2]'), index)
        # 关键字输入
        self.send_keys((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[1]/td/input[1]'), content)
        # 搜索点击
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[1]/td/input[2]'))
        # 下拉框选择
        time.sleep(2)
        self.select_by_index((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[1]/select'), index)
        # 价格输入
        time.sleep(2)
        self.clear((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[2]/p[1]/input'))
        self.send_keys((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[2]/p[1]/input'), price)
        # 前进
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[2]/p[2]/input'))
        # 选择
        time.sleep(2)
        self.select_by_index((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[3]/select'), index)
        # 后退
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[2]/p[3]/input'))
        # 前进
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[2]/p[2]/input'))
        # 选择
        time.sleep(2)
        self.select_by_index((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[3]/select'), index)
        # 双退
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="groupgoods-table"]/tbody/tr[3]/td[2]/p[4]/input'))

    # 关联文章
    def related_articles(self, content, index):
        # 点击关联
        self.click((By.ID, 'article-tab'))
        # 标题输入
        self.send_keys((By.XPATH, '//*[@id="article-table"]/tbody/tr[1]/td/input[1]'), content)
        # 搜索点击
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="article-table"]/tbody/tr[1]/td/input[2]'))
        # 下拉框选择
        time.sleep(2)
        self.select_by_index((By.XPATH, '//*[@id="article-table"]/tbody/tr[3]/td[1]/select'), index)
        # 双进
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="article-table"]/tbody/tr[3]/td[2]/p[1]/input'))
        # 双退
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="article-table"]/tbody/tr[3]/td[2]/p[4]/input'))
        # 前进
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="article-table"]/tbody/tr[3]/td[2]/p[2]/input'))
        # 选择
        time.sleep(2)
        self.select_by_index((By.XPATH, '//*[@id="article-table"]/tbody/tr[3]/td[3]/select'), index)
        # 后退
        time.sleep(2)
        self.click((By.XPATH, '//*[@id="article-table"]/tbody/tr[3]/td[2]/p[3]/input'))

    # 确定
    def submit(self):
        self.click((By.XPATH, "//*[@id='test']/div/input[2]"))
